package Server;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import com.mysql.jdbc.Connection;

public class CancelOrder {
/**
 * @author Daniel
	 * @param con1 Connection to the database
	 * @param log Array of String which describes users query parameters
	 * @return Vector of objects contaning orders information. its number, its items numbers, its price and time
 */
	public static Vector SearchOredrs(Connection con1,String[] log )
	{		
		Vector a = new Vector<Object>();
		
		try {
			java.sql.Statement stmt = con1.createStatement();
			// execute SQL query to get all the book that match to the input
			java.sql.ResultSet res = stmt.executeQuery("SELECT * FROM orders WHERE (user_name='" + log[1] + "') and (status=1)");

			while (res.next()) {				
				Vector b = new Vector<Object>();
				b.add(res.getInt(2));
				b.add(res.getInt(4));
				b.add(res.getInt(7));
				b.add(res.getString(14));				
				a.add(b);
			}
			
			
			res.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
			
		return a;	
	}
	/**
	 * @author Daniel
	 * @param con1 Connection to the database
	 * @param log Array of String which describes users query parameters: orders number and the user name who made the order
	 * @return Array of String describing orders Time, Status and Price
	 */
	public static String[] CancelOredrs(Connection con1,String[] log )
	{		
		String[] data = new String[3];
		
		try {
			java.sql.Statement stmt = con1.createStatement();
			// execute SQL query to get all the book that match to the input
			 
			
			java.sql.ResultSet res = stmt.executeQuery("SELECT * FROM orders WHERE (order_num=" + log[1] + ") and (user_name='"+log[2]+"') and (status=1)");
			if (res.next()) {				
				data[0]=res.getString(13);
				data[1]=res.getString(14);
				data[2]=Integer.toString(res.getInt(7));
				stmt.executeUpdate("UPDATE orders SET status=0 WHERE order_num=" + log[1] + "");
			}
			else
				data[0]="no_entery";
			res.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
			
		return data;
	}
	/**
	 * @author Daniel
	 * @param con1 Connection to the database
	 * @param log Array of String which describes users query parameters
	 * @return Integer whith clients balance account
	 */
	public static int checkbalance(Connection con1,String[] log )
	{		
				
		int balance=0;					
		try {
			java.sql.Statement stmt = con1.createStatement();
			// execute SQL query to get all the book that match to the input
			java.sql.ResultSet res = stmt.executeQuery("SELECT * FROM clients WHERE User_name='" + log[1] + "'");
			if (res.next()) {				
				balance=res.getInt(9);
			}
			res.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (balance);	
	}
	
}
